Repository navigation
Let threads joining main finish when main ends - #49
Conversation
|
No actionable comments were generated in the recent review. Summary
Merge Risk:
|
There was a problem hiding this comment.
The PR fixes a deadlock when a non-daemon thread joins the main thread and main ends with an uncaught exception. The fix moves exitThread(t) before the loop that waits for other non-daemon threads, so main becomes joinable (terminated state, joiners woken) before the VM waits for the remaining threads. This matches HotSpot's behavior where the launcher detaches main before DestroyJavaVM waits for other threads. A new test MainThreadExit.java verifies the scenario, and UncaughtExceptionsTest.java now passes.
review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict approve
A thread that joins the main thread no longer deadlocks when main ends. OpenJDK's
java/lang/Thread/UncaughtExceptionsTest.javahung on this: itsMainThreadAbruptTerminationcase starts a non-daemon thread that joins main, and main ends with an uncaught exception.The VM ran the uncaught exception handler correctly, then waited for the other non-daemon threads before it marked main terminated and woke its joiners. The joining thread waited for main, and main waited for it. Main now exits (
Thread.exit, terminated state, joiners woken) before the wait, as HotSpot's launcher detaches main beforeDestroyJavaVMwaits for the remaining threads. An uncaught exception on main still exits with status 1.Tests:
test/java/MainThreadExit.javamatches HotSpot: a non-daemon thread joins main, and main ends with an uncaught exception.UncaughtExceptionsTest.javamatch HotSpot's output and exit status; the test passes and goes intopassing.txt.pnpm typecheckandpnpm testpass.